package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.LightMsg;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.LightPb;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/12/23
 */
public class LightMsgMgr extends TempMgr {

    public static Map<String, List<LightMsg>> listMap;

    @Override
    public boolean reloadData() throws Exception {
        boolean needNeedNotify = listMap == null ? false : true;
        listMap = ServerBussiness.getLightMsg(ServerListMgr.getServerInfo(GameServer.getInstance().getServerId()).getChannelId(), GameServer.getInstance().getServerId());
        if (needNeedNotify) {
            notifyLightMsg();
        }
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    public static void notifyLightMsg() {
        for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
            sendLightMsgList(player);
        }
    }

    public static void sendLightMsgList(GamePlayer player) {
        List<LightMsg> lightMsgList = LightMsgMgr.getLightMsg(player.getUserInfo().getLanguage());
        player.sendPacket(ClientProtocol.U_NOTIFY_LIGHT_MSG, LightPb.getListMsgList(lightMsgList));
    }

    public static List<LightMsg> getLightMsg(String language) {
        List<LightMsg> list = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        if (listMap.containsKey(language)) {
            for (LightMsg lightMsg : listMap.get(language)) {
                if (nowTime >= lightMsg.getStartTime() && nowTime <= lightMsg.getEndTime()) {
                    list.add(lightMsg);
                }
            }
        }
        return list;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }
}
